package learn;

/**
 *
 */
public class ZuiYouFeiLingWei {
    public static void main(String[] args) {

        int[] arr = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8};
        //System.out.println(ZuiYouFeiLingWei.bit1Count(15));
        ZuiYouFeiLingWei.ab(arr );
    }

    /**
     * 统计bit上1的个数
     *
     * @param num 给定的数组
     * @return 统计bit上1的个数
     */
    public static int bit1Count(int num) {
        int count = 0;
        while (num != 0) {
            int rightOne = num & (~num + 1);
            num ^= rightOne;
            count++;
        }
        return count;
    }

    /**
     * arr中有两种数，出现了奇数次，找到这两个数a,b
     */
    public static void ab(int[] arr) {
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        //eor = a ^ b
        int rightOne = eor & (~eor + 1);
        int a = 0;
        for (int i = 0; i < arr.length; i++) {
            if ((rightOne & arr[i]) == 0) {
                a ^= arr[i];
            }
        }
        System.out.println("a:" + a + "   b:" + (a ^ eor));

    }
}
